package master.userInterface;

import javax.swing.*;

import master.QuantumNetwork;
import master.event.*;

import java.awt.Color;
import java.awt.event.*;

public class NetworkButtonToolBar extends JToolBar implements ActionListener, NetworkEventListener{
	QuantumNetwork myNetwork;
	NetworkScriptPanel myPanel;
	JButton [] buttons = {
			new JButton("Step(0)"),
			new JButton("Reset"),
			new JButton("Back"),
			new JButton("Run")
	};
	JButton errorIndicator;
	public NetworkButtonToolBar(QuantumNetwork k, NetworkScriptPanel myPanel){
		myNetwork = k;
		this.myPanel = myPanel;
		myNetwork.addNetworkListener(this);
		setFloatable(false);
		for(JButton b : buttons){
			this.add(b);
			b.addActionListener(this);
		}
		errorIndicator = new JButton("!");
		errorIndicator.setEnabled(false);
		add(errorIndicator);
	}
	
	public void actionPerformed(ActionEvent e) {
		errorIndicator.setBackground(UIManager.getColor("Button.background"));
		errorIndicator.setText("!");
		try{
			if(e.getSource().equals(buttons[0])){
				myNetwork.getStep(myNetwork.getCurrentStep());
				buttons[0].setText("Step("+myNetwork.getCurrentStep()+")");
			}else if(e.getSource().equals(buttons[1])){
				myNetwork.reset();
			}else if(e.getSource().equals(buttons[2])){
				myNetwork.getStep(myNetwork.getCurrentStep()-2);
			}else if(e.getSource().equals(buttons[3])){
				myNetwork.getStep(myNetwork.getSteps()-1);
			}
		}catch(IllegalArgumentException err){
			errorIndicator.setBackground(Color.red);
			errorIndicator.setText(err.getMessage());
			errorIndicator.setToolTipText(err.getMessage());
		}
	}

	
	public void networkEvent(NetworkEvent e) {
		buttons[0].setText("Step("+myNetwork.getCurrentStep()+")");
	}
}
